// pages/address/address.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      addresslist:"",
    },
    newaddress_fn() {
        wx.navigateTo({
            url: '../inmars/inmars',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      let addresslist = wx.getStorageSync('addresss')
      this.setData({
        addresslist
      })
      if (options.user) {

        console.log("options", options)
            // 从地址信息跳转过来的

        let {
            user,
            value,
            phone,
            address,
            plate,
            index,
            state,
        } = options
        // console.log(state)
        // 从地址栏中返回，没有携带商品信息

        let orders = wx.getStorageSync('orders')
            // let newaddress = wx.getStorageSync('newaddress')
        this.setData({
            orders,
            // newaddress
        })
        let newaddress = address + ' ' + user + ' ' + phone + ' ' + value + ' ' + plate
            // console.log(newaddress)
        wx.setStorageSync('newaddress', newaddress)
        this.setData({
            user,
            phone,
            address,
            orders,
            newaddress,
            plate,
            state,
        })
        let addressArr = wx.getStorageSync('addresslist') || [];
        // console.log(addressArr)
        // console.log(index)
        if (addressArr) {
            if (index >= 0) {
                console.log(this.data.state)
                if (this.data.state == 'delete') {
                    console.log('删除了', index, addressArr)
                        // addressArr.splice(index, 1)
                    console.log(addressArr)
                    this.setData({
                        newaddress: addressArr[0]
                    })
                    wx.setStorageSync('newaddress', this.data.newaddress)
                } else if (this.data.state == 'edit') {
                    addressArr.splice(index, 1, this.data.newaddress)
                }
            } else {
                let boolitem = addressArr.find((item) => {
                    return item == newaddress
                })
                if (!boolitem) {
                    addressArr.push(newaddress)
                }
                // console.log("222")
            }
            this.setData({
                addresslist: addressArr
            })
            wx.setStorageSync('addresslist', this.data.addresslist)
            this.getnum()
        }
    }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})